<script setup>
import {
  Promotion,
  UserFilled,
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom, House, Guide, Expand, Fold
} from '@element-plus/icons-vue'
import {adminInfoService} from '@/api/admin.js'
import useUserInfoStore from '@/stores/userInfo.js'
import {useTokenStore} from '@/stores/token.js'
const tokenStore = useTokenStore();
const userInfoStore = useUserInfoStore();
//调用函数,获取用户详细信息
const getAdminInfo = async()=>{
  //调用接口
  let result = await adminInfoService();
  //数据存储到pinia中
  userInfoStore.setInfo(result.data);
}
//用户图片
import avatar from '@/assets/images/default.png'

//调用函数
getAdminInfo()
//command指令
import {useRouter} from 'vue-router'
const router = useRouter();
import {ElMessage,ElMessageBox} from 'element-plus'
import {ref} from "vue";
const handleCommand = (command)=>{
  //判断指令
  if(command === 'logout'){
    //退出登录
    ElMessageBox.confirm(
        '您确认要退出吗?',
        '温馨提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )
      .then(async () => {
        //退出登录
        //1.清空pinia中存储的token以及个人信息
        tokenStore.removeToken()
        userInfoStore.removeInfo()

        //2.跳转到登录页面
        await router.push('/login')
        ElMessage({
          type: 'success',
          message: '退出登录成功',
        })

      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了退出登录',
        })
      })
  }else{
    //路由
    router.push('/admin/'+command)
  }
}

const isCollapse = ref(false) // 默认展开状态
const toggleMenu = () => {
  isCollapse.value = !isCollapse.value // 切换收缩状态
}

</script>

<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside style="width: auto !important;">
      <div class="el-aside__logo"></div>
      <el-menu active-text-color="#ffd04b" background-color="#232323"  text-color="#fff"
               router default-active="2" class="el-menu-vertical-demo" :collapse="isCollapse">
        <el-sub-menu index="1">
          <template #title>
            <el-icon>
              <House />
            </el-icon>
            <span>首页</span>
          </template>
          <el-menu-item index="/other/home">
            <el-icon><Grid /></el-icon>
            <span>工作台</span>
          </el-menu-item>
          <el-menu-item index="/other/controller">
            <el-icon><Tools /></el-icon>
            <span>控制台</span>
          </el-menu-item>
          <el-menu-item index="/other/analysis">
            <el-icon><TrendCharts /></el-icon>
            <span>分析页</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="2">
          <template #title>
            <el-icon>
              <UserFilled />
            </el-icon>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/user/manage">
            <el-icon>
              <User />
            </el-icon>
            <span>用户列表</span>
          </el-menu-item>
          <el-menu-item index="/user/power">
            <el-icon><Key /></el-icon>
            <span>用户权限</span>
          </el-menu-item>
          <el-menu-item index="/user/resetPassword">
            <el-icon>
              <EditPen />
            </el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="3">
          <template #title>
            <el-icon><Notebook /></el-icon>
            <span>文章管理</span>
          </template>
          <el-menu-item index="/article/manage">
            <el-icon>
              <Promotion />
            </el-icon>
            <span>文章列表</span>
          </el-menu-item>
          <el-menu-item index="/article/inspect">
            <el-icon><Document /></el-icon>
            <span>文章审核</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="4">
          <template #title>
            <el-icon><Discount /></el-icon>
            <span>文章标签</span>
          </template>
          <el-menu-item index="/article/category">
            <el-icon><Ticket /></el-icon>
            <span>标签管理</span>
          </el-menu-item>
          <el-menu-item index="/category/inspect">
            <el-icon><SetUp /></el-icon>
            <span>标签审核</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="5">
          <template #title>
            <el-icon><Guide /></el-icon>
            <span>地点管理</span>
          </template>
          <el-menu-item index="/place/list">
            <el-icon><MapLocation /></el-icon>
            <span>地点列表</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="8">
          <template #title>
            <el-icon><ChatLineRound /></el-icon>
            <span>评论管理</span>
          </template>
<!--          <el-menu-item index="/comment/userComment">-->
<!--            <el-icon><CaretRight /></el-icon>-->
<!--            <span>用户评论</span>-->
<!--          </el-menu-item>-->
          <el-menu-item index="/comment/placeComment">
            <el-icon><CaretRight /></el-icon>
            <span>地点评论</span>
          </el-menu-item>
          <el-menu-item index="/comment/articleComment">
            <el-icon><CaretRight /></el-icon>
            <span>文章评论</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="6">
          <template #title>
            <el-icon><Message /></el-icon>
            <span>消息管理</span>
          </template>
          <el-menu-item index="/message/notice">
            <el-icon><ChatLineSquare /></el-icon>
            <span>消息公告</span>
          </el-menu-item>
          <el-menu-item index="/message/feedback">
            <el-icon><Postcard /></el-icon>
            <span>客服留言</span>
          </el-menu-item>
          <el-menu-item index="/message/msg">
            <el-icon><Postcard /></el-icon>
            <span>用户反馈</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="7">
          <template #title>
            <el-icon><Document /></el-icon>
            <span>系统日志</span>
          </template>
          <el-menu-item index="/system/loginlog">
            <el-icon><DocumentAdd /></el-icon>
            <span>登录日志</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <el-button
            :icon="isCollapse ? Expand : Fold"
            @click="toggleMenu"
            class="toggle-button"
            size="large"
        >
        </el-button>
        <!-- 下拉菜单 -->
        <!-- command: 条目被点击后会触发,在事件函数上可以声明一个参数,接收条目对应的指令 -->
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
              <el-avatar :src="userInfoStore.info.adminPic? userInfoStore.info.adminPic:avatar" />
              <div class="demo-border">管理员：{{ userInfoStore.info.adminName? userInfoStore.info.adminName : '未登录'}}</div>
              <el-icon>
                  <CaretBottom />
              </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
              <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
              <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <!-- 中间区域 -->
      <el-main>
     <!--内容展示区-->
        <router-view></router-view>
      </el-main>
      <!-- 底部区域 -->
      <el-footer>后台管理系统</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    &__logo {
      height: 120px;
      background: url('@/assets/images/logo.png') no-repeat center / 120px auto;
      border-radius: 5px;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }
  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
    height: 30px;
  }
  .demo-border {
    margin-left: 10px;
    border: 2px solid var(--el-border-color);
    border-radius: 10px;
    padding: 10px;
  }
}
</style>